<template>
	
	<view class="p30">
		<view style="height: 200rpx;"></view>
		<image src="/static/image/logo2.png" style="width:90px;height:90px;display: block;margin:20px auto;border-radius: 50%;"></image>
		<view style="height: 50rpx;"></view>
		
		<!--生产APP-->
		<form v-if="ENV == 'prod'" @submit="submit">
			
			<view class="form-list mt60">
				<!--<view class="top b">手机号 <text class="colf00">*</text></view>-->
				<input type="number" @input="setMobile" name="user_mobile" :value="user_mobile" placeholder="*请输入手机号" placeholder-style="color: #8f8f8f;" />
			</view>
			<!-- form-list -->
			
			<view class="form-list mt60 pr">
				<!--<view class="top b">手机验证码 <text class="colf00">*</text></view>-->
				<input type="text" name="code" placeholder="*请输入验证码" placeholder-style="color: #8f8f8f;" />
				<view @click="sendCode" class="mobile_code">{{sowCodeText}}</view>
			</view>
			<!-- form-list -->
			
			<view class="form-list mt60">
				<view class="disf">
					<view>
						<checkbox-group name="is_agree">
						  <label  class="checkbox-type">
							<view class="in">
							  <checkbox value="1" class="csize"/>
							</view>
						  </label>
						</checkbox-group>
					</view>
					<view class="disfr-lr" style="width: 100%;">
						<view class="mt15 f30 pr" style="top:6rpx" >
							我同意<text @click="goPage" data-uri="/pages/article/accord" style="color:#2979ff">《用户协议》</text> & 
							<text @click="goPage" data-uri="/pages/article/privacypolicy" style="color:#2979ff">《隐私政策》</text>
						</view>
					    <!--#ifdef MP || MP-WEIXIN-->
						<!--
						<view class="f32 pr" style="top:20rpx;color:#999" @click="goPage" data-uri="/pages/index/index?disagree=1" data-type="2">不同意协议</view>
					    -->
						<!--#endif-->
					</view>
				</view>
			</view>
			
			<!-- form-list -->
			<view style="height: 150rpx;"></view>
			<view class="form-list">
				<button form-type="submit" class="my_button">立即注册/登录</button>
			</view>
			
			<view class=" mt40 col666">
				<view @click="setType" data-type="simu" style="text-align: center;">帐号/密码登录</view>
			</view>
		</form>
		
		<!--模拟器-->
		<form v-else @submit="submit2">
			<view class="form-list mt60">
				<input type="number" name="user_mobile" value="" placeholder="*请输入帐号" placeholder-style="color: #8f8f8f;" />
			</view>
			<!-- form-list -->
			
			<view class="form-list mt60">
				<input type="password" name="password" placeholder="*请输入密码" placeholder-style="color: #8f8f8f;" />
			</view>
			<!-- form-list -->
			
			<view class="form-list mt60">
				<view class="disf">
					<view>
						<checkbox-group name="is_agree">
						  <label  class="checkbox-type">
							<view class="in">
							  <checkbox value="1" class="csize"/>
							</view>
						  </label>
						</checkbox-group>
					</view>
					<view class="disfr-lr" style="width: 100%;">
						<view class="mt15 f30 pr" style="top:6rpx" >
							我同意<text @click="goPage" data-uri="/pages/article/accord" style="color:#2979ff">《用户协议》</text> & 
							<text @click="goPage" data-uri="/pages/article/privacypolicy" style="color:#2979ff">《隐私政策》</text>
						</view>
					    <!--#ifdef MP || MP-WEIXIN-->
						<!--
						<view class="f32 pr" style="top:20rpx;color:#999" @click="goPage" data-uri="/pages/index/index?disagree=1" data-type="2">不同意协议</view>
					    -->
						<!--#endif-->
					</view>
				</view>
			</view>
			
			<view style="height: 150rpx;"></view>
			<view class="form-list">
				<button form-type="submit" class="my_button">登录</button>
			</view>
			
			<view class=" mt40 col666">
				<view @click="setType" data-type="prod" style="text-align: center;">手机验证码登录</view>
			</view>
			
		</form>
		
		<view class="pa" style="bottom:20px;left:0;text-align: center;width: 100%;">
				<u-divider>{{app_name}}{{app_version}}</u-divider>
		</view>
		<loading :loading="not_load"/><!--加载状态-->
		
		<!--用户协议-->
		<u-popup v-model="accordShow" mode="bottom" border-radius="14" height="80%" :closeable="true">
			<view class="mt30 b f34 bbo pb20 popup-title"><text class="ml30">用户协议</text></view>
			<view style="height: 47px;"></view>
			<view class="p30 f32" style="line-height: 180%;"> <rich-text :nodes="content"></rich-text></view>
		</u-popup>
		
	</view>
	
	
	
</template>

<script setup>
import { onLoad, onShow } from '@dcloudio/uni-app';
import { ref } from 'vue';
const ENV=ref('prod'); // 环境：生产 prod,模拟器 simu
//切换登录方式
const setType=(e)=>{
	console.log(e);
	let type=e.currentTarget.dataset.type;
	ENV.value=type;
};
//弹出用户协
const content=ref('');
const accordShow=ref(false);
const showAccord=()=>{
	accordShow.value=true;
};
const user_mobile=ref('');
const sowCodeText=ref('获取验证码');
const isSend=ref(false);
const setInts=ref('');
const app_name=ref('');
const app_version=ref('');
// #ifndef MP || MP-WEIXIN
plus.runtime.getProperty(plus.runtime.appid, (info)=> {
	app_version.value='V'+info.version;
});
// #endif
//页面加载时
const not_load=ref(false);
onLoad(async (options)=>{
	loadPage({},false);
});
//页面显示时执行
onShow(()=>{
	//console.log('语法糖onShow');
});
//获取接品数据
const system_name=ref('');
const version=ref('');
const loadPage= async (options,isPage)=>{
	let ret = await uni.$get('/appapi-article-accord',options);
	if(ret.error_code != 0){
		uni.$utils.showToast(ret.info);
		return;
	}
	not_load.value=true;
	content.value=ret.data.cn_content1;
	app_name.value=ret.data.app_name;
};
/**
 * 模板属性跳转页面
 * @param {Object} e
 */
const goPage=(e)=>{
	uni.$utils.dataSetPage(e);
};
//登录
const showPass=ref(false);
const setShowPassword=()=>{

	if(showPass.value) showPass.value=false;
	else showPass.value=true;
};
const submit=async (e)=>{
	if(not_load.value== false) return;
	var form=e.detail.value;
	if(!form.is_agree[0]){
		uni.$utils.showToast('请同意用户协议和隐私政策');
		return;
	}else{
		form.is_agree=1;
	}
	if(!form.user_mobile){
		uni.$utils.showToast('手机号必填');
		return;
	}
	if(!form.code){
		uni.$utils.showToast('验证码必填');
		return;
	}
	not_load.value=false;
	let ret = await uni.$post('/appapi-auth-loginregall',{},form);
	not_load.value=true;
	if(ret.error_code != 0){
		uni.$utils.showToast(ret.info);
		return;
	}
	/*uni.$vuex.state.globalData.conversationList=[];
	uni.$vuex.state.globalData.imLoginStatus=false;
	uni.$vuex.state.globalData.user={};
	uni.$utils.setCache('tencentimsig',null);*/
	//uni.TIM.logout();
	
	//写入Token
	uni.$setToken(ret.data.access_token,ret.data.refresh_token);
	
	// [安卓环境]用户拒绝权限进入基础服务
	if(uni.$appConfig.IS_ANDROID){
		const isAgreePrivacy = plus.runtime.isAgreePrivacy();
		if (!isAgreePrivacy) {
			uni.$utils.showToast('登录成功',2,()=>{
				uni.$utils.goPage('/pages/base-service/base-service',2);
			});
			return;
		}
	} 
	
	uni.TIM.listen();
	uni.TIM.login();//即时通信监听
	
	uni.$utils.showToast('登录成功',2,()=>{
		//uni.$utils.goPage('/pages/business/business',2);
		uni.$utils.goPage('/pages/user/user',2);
		return;
		if(!ret.data.is_new_user){
			uni.$utils.goPage('/pages/message/message',2);
			return;
		}
		let uri=encodeURIComponent('/pages/message/message');
		uni.$utils.goPage('/pages/user-data/user-data?uri='+uri,1);
	});
}

//帐号密码登录
const submit2=async(e)=>{
	if(not_load.value== false) return;
	var form=e.detail.value;
	if(!form.is_agree[0]){
		uni.$utils.showToast('请同意用户协议和隐私政策');
		return;
	}else{
		form.is_agree=1;
	}
	if(!form.user_mobile){
		uni.$utils.showToast('帐号必填');
		return;
	}
	if(!form.password){
		uni.$utils.showToast('密码必填');
		return;
	}
	not_load.value=false;
	let ret = await uni.$post('/appapi-auth-applogin',{},form);
	not_load.value=true;
	if(ret.error_code != 0){
		uni.$utils.showToast(ret.info);
		return;
	}
	//写入Token
	uni.$setToken(ret.data.access_token,ret.data.refresh_token);
	
	uni.TIM.listen();
	uni.TIM.login();//即时通信监听
	
	uni.$utils.showToast('登录成功',2,()=>{
		//uni.$utils.goPage('/pages/business/business',2);
		uni.$utils.goPage('/pages/user/user',2);
		return;
		if(!ret.data.is_new_user){
			uni.$utils.goPage('/pages/message/message',2);
			return;
		}
		let uri=encodeURIComponent('/pages/message/message');
		uni.$utils.goPage('/pages/user-data/user-data?uri='+uri,1);
	});
};
//////////////////////////////////
const setMobile=(e)=>{
	user_mobile.value=e.detail.value;
};
const sendCode = async ()=>{
	if(isSend.value) return;
	isSend.value=true;
	//发送手机号
	if(!user_mobile.value){
		uni.$utils.showToast('手机号必填');
		isSend.value=false;
		return;
	}
	not_load.value=false;
	let ret=await uni.$get('/appapi-auth-loginregallcode',{user_mobile:user_mobile.value});
	not_load.value=true;
	if(ret.error_code != 0){
		uni.$utils.showToast(ret.info);
		isSend.value=false;
		return;
	}
	let second=90;
	setInts.value=setInterval(function(){
			second--;
			if(second <= 1){
				sowCodeText.value='获取验证码';
				clearInterval(setInts.value);
				isSend.value=false;
			}else{
				sowCodeText.value=(second)+'S';
			}					
	},1000); 
};
</script>

<style lang="scss">
	page{
		background: #ffffff;
		overflow-x:hidden;
		}
	.regster-form{
		width: 90%;
		height: 400px;
		/*background: red;*/
		margin: 0 auto;
		margin-top: 100px;
	}
	.bottoms{
		position: absolute;
		width: 100%;
		bottom: 20rpx;
	}
	.custom_bot{
		background: #000000 !important;
		color:#fff;
	}
	.my_button{
		color:#fff;
		font-weight: normal;
		font-size: 32rpx;
		border:none;
		background: blue;
		background: #2979ff;
		line-height: 220%;
	}
	.mobile_code{
			width: 200rpx;
			border-radius: 20px;
			background: #2979ff;
			text-align: center;
			height: 65rpx;
			line-height: 65rpx;
			color:#fff;
			position: absolute;
			right: 0;
			top:-10rpx;
			z-index:9999;
		}
</style>
